/*OWmq6Ugo6jPE4W7xoi1UXhNbCE1ElH1XXFf2Qv95OS2hz8AZIpqhqTmMbTjApSmV*/
// 编辑后事件及表体联动
import constant from '../../constants';
import ampub from 'ampub';
import fa from 'fa';

const { utils } = ampub;
const { multiLangUtils } = utils;
const { getMultiLangByID } = multiLangUtils;
const { fa_components } = fa;
const { ReferLinkage } = fa_components;
const { referLinkageClear } = ReferLinkage;

export default function afterEvent(that1, props, moduleId, key, value, changedrows, i) {
    const result = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'check_result').value;
    if (value == null && result == null) {
        return;
    }
    // 如果输入的值为空则不处理
    if (result == null && (JSON.stringify(value) == '{}' || value == '')) {
        if (key === 'usedept_after') {
            // 盘点编辑后事件
		    checkInventResult.call(this, that1, props, moduleId, key, value, changedrows, i);
        }
        return;
    }
    if (
        key === 'card_num_after'
		|| key === 'pk_usingstatus_after'
		|| key === 'pk_mandept_after_v'
		|| key === 'pk_mandept_after'
		|| key === 'assetuser_after'
		|| key === 'pk_usedept_after_v'
		|| key === 'pk_usedept_after'
        || key === 'usedept_after'
		|| key === 'pk_ownerorg_after_v'
		|| key === 'position_after'
		|| key === 'pk_equiporg_after_v'
    ) {
        // 盘点编辑后事件
        checkInventResult.call(this, that1, props, moduleId, key, value, changedrows, i);
    }
    if (key === 'pk_dept' || key === 'pk_category') {
        // 部门类别编辑后事件
        chooseMulti(that1, props, moduleId, key, value, changedrows, i);
    }
    if (key === 'pk_ownerorg_after_v' || key === 'pk_equiporg_after_v') {
        referLinkageClear(props, key, constant.DEFAULTRANGECONFIG, 'cardtable', moduleId, i);
    }
    if (key == 'pk_usedept_after_v' && (changedrows[0].newvalue.value == null || changedrows[0].newvalue.value == '')) {
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
            pk_usedept_after: { value: null, display: null },
            'pk_usedept_after_v.name': { value: null, display: null }
        });
    }

    // pk_usedept_after与pk_usedept_after_v值按相同处理
    if (key == 'pk_usedept_after') {
        const value = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'pk_usedept_after').value;
        const display = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'pk_usedept_after').display;
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, { pk_usedept_after_v: { value, display } });
    }

    if (key == 'pk_usedept_after_v') {
        const value = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, key).value;
        const display = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, key).display;
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, { pk_usedept_after: { value, display } });
    }
}
/* 根据盘点后数据给盘点结果赋值 */
function checkInventResult(that1, props, moduleId, key, value, changedrows, i) {
    // 卡片
    const pk_card = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'pk_card').value;
    // 数量(盘点后)
    const card_num_after = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'card_num_after')
        .value;

    // 一、盘盈：
    if (pk_card == undefined || pk_card == null || pk_card == '') {
        // 盘点结果赋值
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
            check_result: {
                display: getMultiLangByID(constant.CHECK_RESULT.PROFIT.display),
                value: constant.CHECK_RESULT.PROFIT.value
            }
        });
        return;
    }
    // 未处理
    if ((card_num_after == '' || card_num_after == null) && key == 'card_num_after') {
        clearItemValue(that1, i);
        return;
    }
    // 二、盘亏：
    if (card_num_after === 0 || card_num_after === '0') {
        // 盘点结果赋值
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
            check_result: {
                display: getMultiLangByID(constant.CHECK_RESULT.REDUCE.display),
                value: constant.CHECK_RESULT.REDUCE.value
            }
        });
        // 所有盘点后信息清除
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
            pk_usedept_after: { value: null, display: null },
            pk_usedept_after_v: { value: null, display: null },
            usedept_after: { value: null, display: null },
            'pk_usedept_after_v.name': { value: null, display: null },
            pk_mandept_after: { value: null, display: null },
            pk_mandept_after_v: { value: null, display: null },
            pk_equiporg_after: { value: null, display: null },
            pk_equiporg_after_v: { value: null, display: null },
            'pk_equiporg_after_v.name': { value: null, display: null },
            pk_ownerorg_after: { value: null, display: null },
            pk_ownerorg_after_v: { value: null, display: null },
            'pk_ownerorg_after_v.name': { value: null, display: null },
            assetuser_after: { value: null, display: null },
            position_after: { value: null, display: null },
            pk_usingstatus_after: { value: null, display: null }
        });
        return;
    }

    // 先判断是否是从盘亏转换到不符，如果是，则需要将清空的盘点后的值赋上盘点前的值
    // if (key == 'card_num_after' && changedrows[0].oldvalue.value == '0') {
    // 	evaluateAttribute(that, pk_card, i);
    // 	return;
    // }

    const INVENTORY_ITEMS = getInventoryItems.call(this);
    // 三、不符：
    // 状况（盘点后）
    for (const key in INVENTORY_ITEMS.WITH_V) {
        // 使用部门单独处理
        if (key == 'pk_usedept_after_v') {
            // 使用部门特殊处理
            const pk_usedept_after_v = that1.props.cardTable.getValByKeyAndIndex(
                constant.AREA.STEP.DETAIL_LIST,
                i,
                'pk_usedept_after_v'
            ).display;
            const pk_usedept_before_code = that1.props.cardTable.getValByKeyAndIndex(
                constant.AREA.STEP.DETAIL_LIST,
                i,
                'pk_usedept_before_code'
            ).value;
            if (pk_usedept_before_code != pk_usedept_after_v) {
                that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
                    check_result: {
                        display: getMultiLangByID(constant.CHECK_RESULT.NOT_EQUAL.display),
                        value: constant.CHECK_RESULT.NOT_EQUAL.value
                    }
                });
                return;
            }
            continue;
        }
        const item_value_before = that1.props.cardTable.getValByKeyAndIndex(
            constant.AREA.STEP.DETAIL_LIST,
            i,
            `${INVENTORY_ITEMS.WITH_V[key].slice(0, INVENTORY_ITEMS.WITH_V[key].length - 'after_v'.length)}before_v`
        ).value;
        const item_value_after = that1.props.cardTable.getValByKeyAndIndex(
            constant.AREA.STEP.DETAIL_LIST,
            i,
            INVENTORY_ITEMS.WITH_V[key].slice(0, INVENTORY_ITEMS.WITH_V[key].length)
        ).value;
        if (
            (item_value_before == null && item_value_after != null && item_value_after != '')
			|| (item_value_before != null && item_value_before != item_value_after)
        ) {
            that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
                check_result: {
                    display: getMultiLangByID(constant.CHECK_RESULT.NOT_EQUAL.display),
                    value: constant.CHECK_RESULT.NOT_EQUAL.value
                }
            });
            if ((card_num_after == '' || card_num_after == null) && key != 'card_num_after') {
                const num = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'card_num_before')
                    .value;
                that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
                    card_num_after: {
                        value: num,
                        display: num
                    }
                });
            }
            return;
        }
    }
    for (const key in INVENTORY_ITEMS.UNWITH_V) {
        // 使用部门单独处理
        if (key == 'pk_usedept_after') {
            // 使用部门特殊处理
            const pk_usedept_after = that1.props.cardTable.getValByKeyAndIndex(
                constant.AREA.STEP.DETAIL_LIST,
                i,
                'pk_usedept_after'
            ).display;
            const pk_usedept_before_code = that1.props.cardTable.getValByKeyAndIndex(
                constant.AREA.STEP.DETAIL_LIST,
                i,
                'pk_usedept_before_code'
            ).value;
            if (pk_usedept_before_code != pk_usedept_after) {
                that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
                    check_result: {
                        display: getMultiLangByID(constant.CHECK_RESULT.NOT_EQUAL.display),
                        value: constant.CHECK_RESULT.NOT_EQUAL.value
                    }
                });
                return;
            }
            continue;
        }
        const item_value_before = that1.props.cardTable.getValByKeyAndIndex(
            constant.AREA.STEP.DETAIL_LIST,
            i,
            `${INVENTORY_ITEMS.UNWITH_V[key].slice(0, INVENTORY_ITEMS.UNWITH_V[key].length - 'after'.length)}before`
        ).value;
        const item_value_after = that1.props.cardTable.getValByKeyAndIndex(
            constant.AREA.STEP.DETAIL_LIST,
            i,
            INVENTORY_ITEMS.UNWITH_V[key]
        ).value;
        if (
            (item_value_before == null && item_value_after != null && item_value_after != '')
			|| (item_value_before != null && item_value_before != item_value_after)
        ) {
            that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
                check_result: {
                    display: getMultiLangByID(constant.CHECK_RESULT.NOT_EQUAL.display),
                    value: constant.CHECK_RESULT.NOT_EQUAL.value
                }
            });
            return;
        }
    }

    // 四、相符
    // 盘点结果赋值
    that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
        check_result: {
            display: getMultiLangByID(constant.CHECK_RESULT.EQUAL.display),
            value: constant.CHECK_RESULT.EQUAL.value
        }
    });
    if ((card_num_after == '' || card_num_after == null) && key != 'card_num_after') {
        const num = that1.props.cardTable.getValByKeyAndIndex(constant.AREA.STEP.DETAIL_LIST, i, 'card_num_before').value;
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
            card_num_after: {
                value: num,
                display: num
            }
        });
    }
}

// function evaluateAttribute(that1, pk_card, i) {
//     const data = { allpks: [pk_card] };
//     ajax({
//         url: constant.URL.QUERY_CARDMESSAGE,
//         data,
//         success: (res) => {
//             const { success, data } = res;
//             if (success) {
//                 if (data) {
//                     const deptScaleVO = data[pk_card];
//                     let pk_usedept_after_value = '';
//                     let pk_usedept_after_v_value = '';
//                     if (deptScaleVO) {
//                         if (deptScaleVO.pk_dept) {
//                             pk_usedept_after_value = deptScaleVO.pk_dept;
//                         }
//                         if (deptScaleVO.pk_dept_v) {
//                             pk_usedept_after_v_value = deptScaleVO.pk_dept_v;
//                         }
//                     }
//                     const rows = that1.props.cardTable.getRowsByIndexs(constant.AREA.STEP.DETAIL_LIST, i);
//                     const hiddenItems = getInventoryHiddenItems(that1.props);
//                     // 获取隐藏的字段进行赋值操作
//                     for (const key in hiddenItems) {
//                         if (key == 'pk_usedept_after_v') {
//                             that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
//                                 pk_usedept_after_v: {
//                                     display: rows[0].values.pk_usedept_before_code.value,
//                                     value: pk_usedept_after_v_value
//                                 }
//                             });
//                             continue;
//                         }
//                         if (key == 'pk_usedept_after') {
//                             that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
//                                 pk_usedept_after: {
//                                     display: rows[0].values.pk_usedept_before_code.value,
//                                     value: pk_usedept_after_value
//                                 }
//                             });
//                             continue;
//                         }
//                         const after = hiddenItems[key];
//                         const before = after.replace('after', 'before');
//                         that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
//                             [after]: {
//                                 display: rows[0].values[before].display,
//                                 value: rows[0].values[before].value
//                             }
//                         });
//                     }
//                     that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, i, {
//                         check_result: {
//                             display: getMultiLangByID(constant.CHECK_RESULT.NOT_EQUAL.display),
//                             value: constant.CHECK_RESULT.NOT_EQUAL.value
//                         }
//                     });
//                 }
//             }
//         }
//     });
// }

function getInventoryItems() {
    const INVENTORY_ITEMS = JSON.parse(JSON.stringify(constant.INVENTORY_ITEMS));
    const meta = this.props.meta.getMeta()[constant.AREA.STEP.DETAIL_LIST].items;
    meta.forEach((meta_item) => {
        for (const key in INVENTORY_ITEMS.WITH_V) {
            if (meta_item.attrcode == INVENTORY_ITEMS.WITH_V[key]) {
                if (meta_item.visible == false) {
                    delete INVENTORY_ITEMS.WITH_V[key];
                }
            }
        }
        for (const key in INVENTORY_ITEMS.UNWITH_V) {
            if (meta_item.attrcode == INVENTORY_ITEMS.UNWITH_V[key]) {
                if (meta_item.visible == false) {
                    delete INVENTORY_ITEMS.UNWITH_V[key];
                }
            }
        }
    });
    return INVENTORY_ITEMS;
}

export function getInventoryHiddenItems(props) {
    const INVENTORY_ISHIDDEN_ITEMS = JSON.parse(JSON.stringify(constant.INVENTORY_ISHIDDEN_ITEMS));
    const meta = props.meta.getMeta()[constant.AREA.STEP.DETAIL_LIST].items;
    meta.forEach((meta_item) => {
        for (const key in INVENTORY_ISHIDDEN_ITEMS) {
            if (meta_item.attrcode == INVENTORY_ISHIDDEN_ITEMS[key]) {
                if (meta_item.visible == true) {
                    delete INVENTORY_ISHIDDEN_ITEMS[key];
                }
            }
        }
    });
    return INVENTORY_ISHIDDEN_ITEMS;
}

/* 部门类别多选 */
function chooseMulti(that1, props, moduleId, key, value, changedrows, i) {
    if (that1.state.current == 1) {
        if (key === 'pk_category') {
            that1.props.editTable.setValByKeyAndIndex(constant.AREA.STEP.ALLOT_CHECKUSER, i, 'pk_category', { value: true });
        }
        if (key === 'pk_dept') {
            if (value[0].runWithChildren || value[0].runWithChildren == true) {
                that1.props.editTable.setValByKeyAndIndex(constant.AREA.STEP.ALLOT_CHECKUSER, i, 'pk_dept', { value: true });
            }
        }
    }
}

function clearItemValue(that1, index) {
    const WITH_V = JSON.parse(JSON.stringify(constant.INVENTORY_ITEMS.WITH_V));
    for (const invkey in WITH_V) {
        const obj = {};
        obj[invkey] = {
            value: null,
            display: null
        };
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, index, obj);
    }
    const UNWITH_V = JSON.parse(JSON.stringify(constant.INVENTORY_ITEMS.UNWITH_V));
    delete UNWITH_V.card_num_after;
    for (const invkey in UNWITH_V) {
        const obj = {};
        obj[invkey] = {
            value: null,
            display: null
        };
        that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, index, obj);
    }
    that1.props.cardTable.setValByKeysAndIndex(constant.AREA.STEP.DETAIL_LIST, index, {
        check_result: {
            value: null,
            display: null
        }
    });
}

/*OWmq6Ugo6jPE4W7xoi1UXhNbCE1ElH1XXFf2Qv95OS2hz8AZIpqhqTmMbTjApSmV*/